import 'package:emphasize_word/card.dart';
import 'package:emphasize_word/provider.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

import 'package:provider/provider.dart';

class Content extends StatefulWidget {
  const Content({Key? key}) : super(key: key);

  @override
  _ContentState createState() => _ContentState();
}

class _ContentState extends State<Content> {
  @override
  void initState() {
    new Future.delayed(Duration.zero, () {
      Provider.of<ListSave>(context, listen: false).getData(context, -1); //初始请求
    });
    super.initState();
  }

  _buildButtons(ListSave provider) {
    var tmp = provider.total_index;
    List<Widget> widgets = [];
    tmp.forEach((ele) {
      widgets.add(
        ElevatedButton(
          onPressed: () {
            provider.getData(context, ele);
            provider.setNumber(ele);
          },
          child: Text(
            "List$ele",
            style: TextStyle(
              fontWeight: provider.now_number == ele
                  ? FontWeight.w400
                  : FontWeight.w400,
            ),
          ),
          style: ButtonStyle(
            padding: MaterialStateProperty.all(EdgeInsets.only(
              left: 5.0,
              right: 5.0,
              top: 10,
              bottom: 10,
            )),
            foregroundColor: MaterialStateProperty.all(
              provider.now_number == ele ? Colors.black : Colors.grey[400],
            ),
            backgroundColor: MaterialStateProperty.all(Colors.transparent),
            shadowColor: MaterialStateProperty.all(Colors.transparent),
          ),
        ),
      );
    });
    return widgets;
  }

  @override
  Widget build(BuildContext context) {
    ListSave provider = Provider.of<ListSave>(context);
    Schedule provider_schedule = Provider.of<Schedule>(context);
    Done provider_done = Provider.of<Done>(context);
    return Container(
      child: Center(
        child: ListView.builder(
          controller: provider.listViewController,
          physics: BouncingScrollPhysics(),
          itemCount: provider.datalists.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              margin: EdgeInsets.only(top: index == 0 ? 7 : 0),
              child: Column(
                children: [
                  index == 0
                      ? Wrap(children: _buildButtons(provider))
                      : Container(),
                  WordCard(
                    index: index,
                    mydata: provider.sort == 0
                        ? provider.datalists[index]
                        : provider.sorted1_datalists[index],
                    similar: provider.sort == 0
                        ? provider.similars[index]
                        : provider.sorted1_similars[index],
                  ),
                  index == provider.datalists.length - 1
                      ? [
                          GestureDetector(
                            onTap: () async {
                              await Done().setDone(context);
                              await provider.getDoneFlag(context);
                              await provider.getData(context, -1);
                            },
                            child: Padding(
                              padding: const EdgeInsets.all(20.0),
                              child: Text(
                                "学习完成✅，点此完成本list打卡",
                                style: TextStyle(
                                  color: Colors.blue,
                                ),
                              ),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(20.0),
                            child: Text(
                              "已打卡🌟",
                              style: TextStyle(
                                color: Colors.blue,
                              ),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(20.0),
                            child: Text(
                              "请按顺序打卡",
                              style: TextStyle(
                                color: Colors.blue,
                              ),
                            ),
                          ),
                        ][provider.done_flag]
                      : Container(),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
